package com.hmy.cornpestdet.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class FileHelper {
    private static String saveDir = "E:/coding/project/project java/CornPestDet/src/main/resources/static/images/";
    public static String saveFileToDisk(MultipartFile file) throws IOException {
        if(file == null || file.isEmpty())return "";
        String originalFilename = file.getOriginalFilename();
        String extentionName = originalFilename.split("\\.")[1];
        String fileName = UUID.randomUUID().toString() + "." + extentionName;
        File dir = new File(saveDir);
        if(!dir.exists())dir.mkdirs();
        file.transferTo(new File(saveDir + fileName));
        //返回图片的新名字
        return fileName;
    }

    public static List<String> saveFileListToDisk(MultipartFile[] files) throws IOException {
        List<String> fileNewNameList = new ArrayList<>();
        for(MultipartFile file : files){
            String fileNewNam = saveFileToDisk(file);
            fileNewNameList.add(fileNewNam);
        }
        return fileNewNameList;
    }
}
